﻿using MslDiary.BLL;
using MslDiary.Model;
using MslDiary.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Input;

namespace MslDiary.ViewModel
{
    public class AddAttachFileViewModel : MslDiary.Framework.ViewModelBase
    {
        private A_AnnexBLL annexBLL = new A_AnnexBLL();
        private System.Windows.Window win = null;
        public AddAttachFileViewModel()
        {
            this.WindowLoadCommand = new CommandBase((Object window) =>
            {
                Load();
            });
            this.SaveCommand = new CommandBase((Object window) =>
            {
                if (AddAttachFile())
                {
                    this.FilePath = String.Empty;
                    this.ShowName = String.Empty;
                    this.win = (System.Windows.Window)window;
                    if (this.win != null)
                        this.win.Hide();
                }
            });
            this.BrowseFileCommand = new CommandBase((Object m) => BrowseFile());
            this.WindowLoadCommand = new CommandBase((Object m) => Load());
            this.CancleCommand = new CommandBase((Object window) =>
            {
                this.win = (System.Windows.Window)window;
                if (this.win != null)
                    this.win.Hide();
            });
        }

        #region 属性

        private string _path;
        private string _showName;
        private int _state;
        private int _fromRecordId;
        /// <summary>
        /// 状态 0：更新或添加未成功，1：添加成功，2：更新成功
        /// </summary>
        public int State
        {
            get { return _state; }
            set
            {
                //base.SetProperty<int>(ref this._state, value);
                if (this._state == value)
                    return;
                this._state = value;
                this.RaisePropertyChanged("State");
            }
        }
        public int FromRecordId
        {
            get { return _fromRecordId; }
            set
            {
                //base.SetProperty<int>(ref this._fromRecordId, value);
                if (this._fromRecordId == value)
                    return;
                this._fromRecordId = value;
                this.RaisePropertyChanged("FromRecordId");
            }
        }
        public string ShowName
        {
            get { return _showName; }
            set
            {
                //base.SetProperty<string>(ref this._showName, value);
                if (this._showName == value)
                    return;
                this._showName = value;
                this.RaisePropertyChanged("ShowName");
            }
        }
        public string FilePath
        {
            get { return _path; }
            set
            {
                //base.SetProperty<string>(ref this._path, value);
                if (this._path == value)
                    return;
                this._path = value;
                this.RaisePropertyChanged("FilePath");
            }
        }
        public A_Annex AnnexModel { get; set; }
        #endregion

        #region 命令
        /// <summary>保存</summary>
        public ICommand SaveCommand { get; set; }
        /// <summary>选择文件</summary>
        public ICommand BrowseFileCommand { get; set; }
        /// <summary>取消</summary>
        public ICommand CancleCommand { get; set; }
        /// <summary></summary>
        public ICommand WindowLoadCommand { get; set; }

        #endregion

        #region 方法

        private void Load()
        {
            this.State = 0;
            this.ShowName = String.Empty;
            this.FilePath = String.Empty;
        }
        private void BrowseFile()
        {
            this.State = 0;
            using (System.Windows.Forms.OpenFileDialog dialog =
                 new System.Windows.Forms.OpenFileDialog())
            {
                dialog.Filter = "文档|*.doc;*.docx;*.xls;*.xls;*.xlsx;*.ppt;*.pptx;*.pdf|压缩格式|*.zip;*.rar|视频|*.avi;*.wmv;*.flv|图片|*.jpg;*.png";
                dialog.FilterIndex = 0;
                if (System.Windows.Forms.DialogResult.OK == dialog.ShowDialog())
                {
                    this.FilePath = dialog.FileName;
                    this.ShowName = this.FilePath.Substring(this.FilePath.LastIndexOf("\\") + 1).Split('.')[0];
                    FileInfo fi = new FileInfo(this.FilePath);
                    if (fi.Length / 1024 > 5120)// 5 MB
                        MslDiary.Utils.Helper.Instance.AlertWarning("上传附件不能超过 5 MB，请重新选择！");
                }
            }
        }
        /// <summary>
        /// 添加附件
        /// </summary>
        private bool AddAttachFile()
        {
            if (String.IsNullOrEmpty(this.FilePath) || String.IsNullOrEmpty(this.ShowName))
            {
                MslDiary.Utils.Helper.Instance.AlertWarning("显示文件名或文件路径不能为空！");
                return false;
            }
            if (!File.Exists(this.FilePath))
            {
                MslDiary.Utils.Helper.Instance.AlertWarning("文件不存在，请重新选择！");
                return false;
            }
            FileInfo fi = new FileInfo(this.FilePath);
            if (fi.Length / 1024 > 5120)// 5 MB
            {
                MslDiary.Utils.Helper.Instance.AlertWarning("上传附件不能超过 5 MB，请重新选择！");
                return false;
            }
            this.AnnexModel = new A_Annex();
            this.AnnexModel.Name = fi.Name;
            this.AnnexModel.Suffix = "." + fi.Name.Split('.')[1];
            this.AnnexModel.FileType = MslDiary.Framework.Helper.GetFileType(this.AnnexModel.Suffix);
            this.AnnexModel.FileSize = fi.Length / 1024;
            this.AnnexModel.FromTable = "A_Diary";
            this.AnnexModel.FromField = "";
            this.AnnexModel.ShowName = this.ShowName;
            this.AnnexModel.FromRecordId = this.FromRecordId;
            this.AnnexModel.CreateDate = DateTime.Now;
            this.AnnexModel.Content = MslDiary.Framework.Helper.FileToByte(this.FilePath);
            try
            {
                if (annexBLL.Add(this.AnnexModel) > 0)
                {
                    this.State = 1;
                    MslDiary.Utils.Helper.Instance.AlertSuccess(MslDiary.Framework.ShowMessage.SAVE_MSG01);
                    return true;
                }
                else
                    MslDiary.Utils.Helper.Instance.AlertWarning(MslDiary.Framework.ShowMessage.SAVE_MSG02);
            }
            catch (Exception)
            {
                MslDiary.Utils.Helper.Instance.AlertError(MslDiary.Framework.ShowMessage.SYS_MSG01);
            }
            return false;
        }
        #endregion
    }
}
